package cn.lf.android.lawfirm.login.controller;

import android.content.Context;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.lf.android.lawfirm.AppConfig;
import cn.lf.android.lawfirm.BaseController;
import cn.lf.android.lawfirm.MyApplication;
import cn.lf.android.lawfirm.http.manager.EBCallback;
import cn.lf.android.lawfirm.http.manager.MyOkHttpClient;
import cn.lf.android.lawfirm.login.UserAccount;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.SPUtil;

import static android.R.attr.id;
import static cn.lf.android.lawfirm.AppConfig.TokenAuth_LoginIn;


public class LoginController extends BaseController {
    LoginCallback mLoginCallback;
    private Context context;

    public LoginController(LoginCallback callback, Context context) {
        this.mLoginCallback = callback;
        this.context = context;
    }

    public void onLogin(final String UserName, final String Password) {
//        showProgressDia(context);
        Map<String, String> params = new HashMap<String, String>();
        params.put("UserName", UserName);
        params.put("Password", Password);
        new MyOkHttpClient().doPost(AppConfig.LOGIN, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("登录=" + response);
                        JSONObject myJsonObject;
                        JSONObject responsObject;
                        Boolean isSuc;
                        String accessToken;
                        String errorMsg;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            errorMsg = myJsonObject.getString("errorMsg");
                            if (isSuc) {
                                responsObject = myJsonObject.getJSONObject("responsObject");
                                accessToken = responsObject.getString("accessToken");
                                MyApplication.getInstance().setJPAlias(responsObject.getString("userId"));
                                SPUtil spUtil = new SPUtil(context);
                                UserAccount userAccount = new UserAccount();
                                userAccount.setUserName(UserName);
                                userAccount.setPassword(Password);
                                userAccount.setUserId(responsObject.getString("userId"));
                                spUtil.setAccountInfo(userAccount);
                                spUtil.setAccessToken("Bearer " + accessToken);
                                mLoginCallback.onSuccessed(accessToken);
                            } else {
                                mLoginCallback
                                        .onFailed(errorMsg);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
//                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        mLoginCallback.onFailed("用户名或密码无效");
//                        dismissProgressDia();
                    }
                });
    }

    public void onLogin(final String UserName, final String Password, final String RealPassword, String TimeStamp, Boolean isShow) {
        if (isShow) {
            showProgressDia(context);
        }
        Map<String, String> params = new HashMap<String, String>();
        params.put("appkey", "wfWr");
        params.put("UserName", UserName);
        params.put("Password", Password);
        params.put("TimeStamp", TimeStamp);
        DebugLog.e("TokenAuth_LoginIn==" + AppConfig.TokenAuth_LoginIn);
        DebugLog.e("UserNamemd==" + UserName);
        DebugLog.e("Passwordmd==" + Password);
        DebugLog.e("TimeStampmd==" + TimeStamp);

        new MyOkHttpClient().doPost(AppConfig.TokenAuth_LoginIn, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("登录=" + response);
                        JSONObject myJsonObject;
                        JSONObject responsObject;
                        Boolean isSuc;
                        String accessToken;
                        String errorMsg;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            errorMsg = myJsonObject.getString("errorMsg");
                            if (isSuc) {
                                responsObject = myJsonObject.getJSONObject("responsObject");
                                accessToken = responsObject.getString("accessToken");
                                MyApplication.getInstance().setJPAlias(responsObject.getString("userId"));
                                SPUtil spUtil = new SPUtil(context);
                                UserAccount userAccount = new UserAccount();
                                userAccount.setUserName(UserName);
                                userAccount.setPassword(RealPassword);
                                userAccount.setUserId(responsObject.getString("userId"));
                                spUtil.setAccountInfo(userAccount);
                                spUtil.setAccessToken("Bearer " + accessToken);
                                mLoginCallback.onSuccessed(accessToken);
                            } else {
                                mLoginCallback
                                        .onFailed(errorMsg);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        mLoginCallback.onFailed("用户名或密码无效");
                        dismissProgressDia();
                    }
                });
    }

    /**
     * 登录回调接口
     */
    public interface LoginCallback {
        /**
         * 登录请求成功
         */
        public void onSuccessed(String accessToken);

        /**
         * 登录请求失败
         */
        public void onFailed(String errorMsg);

    }
}
